package com.itheima.manager_system.teacher_service;

import com.itheima.manager_system.domain.Person;
import com.itheima.manager_system.domain.Teacher;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class SalaryManager {
    private Scanner sc = new Scanner(System.in);

    public void SalaryMenu(ArrayList<Person> list){
        String select;

        while (true) {
            System.out.println("-------------------------------------------");
            System.out.println("1 录入/修改工资信息  2 查询工资 3 返回上一层 ");
            System.out.println("-------------------------------------------");
            select = sc.next();
            switch (select) {
                case "1":
                    setSalary(list);
                    break;
                case "2":
                    allSalary(list);
                    break;
                case "3":
                    return;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    private void setSalary(ArrayList<Person> list) {
        System.out.println("请输入要录入工资的老师编号:");
        int id = sc.nextInt();
        int index = getIndex(list,id);
        if(index==-1){
            System.out.println("编号"+id+"的老师不存在");
            return;
        }
        Teacher te =(Teacher) list.get(index);
        System.out.println("编号"+id+"\t"+ te.getName()+"\t"+"录入前");
        System.out.println("工龄:"+ te.getWorkingAge()+"\t"
                +"工资:"+ te.getSalary());

        System.out.println("请输入工龄");
        te.setWorkingAge(sc.nextInt());
        System.out.println("请输入工资:");
        te.setSalary(sc.nextDouble());
        System.out.println("录入成功");

    }

    public void allSalary(ArrayList<Person> list){
        if(list.size()==1){
            System.out.println("暂无数据");
            return;
        }
        System.out.println("-------------------------------------------");
        System.out.println("编号\t名字\t工资\t工龄");
        TreeSet<Teacher> salarys = new TreeSet<>(new Comparator<>() {
            @Override
            public int compare(Teacher s1, Teacher s2) {
                int num1= s2.getWorkingAge()-s1.getWorkingAge();
                int num2=num1 ==0?s1.getId()-s2.getId():num1;
                return num2;
            }
        });
        for (Person teacher : list) {
            if (teacher.getId()!=0) {
                Teacher st = (Teacher)teacher;
                salarys.add(st);
            }
        }

        for (Teacher te : salarys) {
            StringBuilder salary = new StringBuilder();
            salary.append(te.getId()).append("\t").append(te.getName())
                    .append("\t").append(te.getSalary()).append("\t")
                    .append(te.getWorkingAge());
            System.out.println(salary);
        }



    }
    //编号所在元素在集合中的索引(不存在则返回-1)
    public int getIndex(ArrayList<Person> list, int id) {
            /*
            编号唯一性的判断方法
            遍历list,用id逐个匹配list中每个元素的id,如果匹配到就返回索引
            如果没有匹配到,最后返回-1
             */
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() == id) {
                return i;
            }
        }
        return -1;
    }
}
